// components/house/house.js
const app = getApp();
const db = wx.cloud.database();
const _ = db.command
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    userInfo: {
      type: Object,
      value: {}
    },    
    home:{
      type: String,
      value: ''
    }
   
  },

  /**
   * 组件的初始数据
   */
  data: {
    globalUser: app.globalData.userInfo,
    room: [
      [390, 240, 240, 0, 0],
      [585, 385, 435, 195, 195],
      [650, 450, 500, 260, 260],
      [750, 550, 600, 360, 360],
      [1000, 800, 850, 610, 610]
    ],
    components_name: ['wrapper', 'main-wall', 'main-roof-gutter', 'main-roof-left', 'main-roof-right'],
   
    houseMenuShow: false,
    houseMenu: [{
        action: 'repair',
        icon: 'actions/repair.svg',
        text: '修缮城堡'
      },
      {
        action: 'house',
        icon: 'actions/house.svg',
        text: '升级城堡'
      },
    ],
  },

  /**
   * 组件的方法列表
   */
  methods: {
    toggleMenu() {
      this.triggerEvent('hideMenu','house')
      this.setData({
        houseMenuShow: this.data.houseMenuShow ? false : true,
      })
    },
    hideMenu(){
      this.setData({
        houseMenuShow:false
      })   
    },
    house() {
      console.log(this.data.userInfo.house.roomSum, this.data.userInfo.gold)

      wx.cloud.callFunction({
        name: 'house',
        data: {
          op: 'house',
          //  _openid:this.data.user._openid
        }
      }).then(res => {
        console.log(res)
        if (res.result == "roomEnough") {
          wx.showToast({
            title: '暂时不能升级房子',
            icon: 'none'
          })
          return false
        }
        if (res.result == "goldNotEnough") {
          wx.showToast({
            title: '金币不够，不能升级',
            icon: 'none'
          })
          return false
        }
        wx.showToast({
          title: '恭喜升级成功，现在有 ' + res.result.roomSum + "间房了",
          icon: 'none'
        })
        this.triggerEvent("getUserInfo");
      })
    
    },
    repair() {
    
      wx.cloud.callFunction({
        name: 'house',
        data: {
          op: 'repair',         
        }
      }).then(res => {
        console.log(res)
        if (res.result == "lifeEnough") {
          wx.showToast({
            title: '暂时不需要修缮',
            icon: 'none'
          })
          return false
        }
        if (res.result == "goldNotEnough") {
          wx.showToast({
            title: '金币不够，无法修缮',
            icon: 'none'
          })
          return false
        }
        wx.showToast({
          title: '修缮完成',
          icon: 'none'
        })
        this.triggerEvent("getUserInfo");
      })
    
    },
  },


})